/**
 * @author Tobias Boehm
 *
 * 28.05.2011
 */
package org.anddev.andengine.extension.tbsengine.message.server.preparematch;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import org.anddev.andengine.extension.tbsengine.match.AbsServerMatchPreparation;
import org.anddev.andengine.extension.tbsengine.match.AbsServerMatchPreparation.MatchPreparationState;
import org.anddev.andengine.extension.tbsengine.message.ICopyable;
import org.anddev.andengine.extension.tbsengine.message.server.AbsServerMatchMessage;

/**
 * You should use this server message to announce a {@link MatchPreparationState}
 * change to the players.
 * 
 * @author Tobias Boehm
 *
 */
public class ServerMatchpreprataionStateChangedAnnouncement extends AbsServerMatchMessage
		implements ICopyable<ServerMatchpreprataionStateChangedAnnouncement> {

	private MatchPreparationState mMatchState;
	
	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.multiplayer.protocol.adt.message.IMessage#getFlag()
	 */
	@Override
	public short getFlag() {
		
		return FLAG_SERVER_MATCHPREPARATION_STATE_CHANGED_ANNOUNCEMENT;
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.shared.ICopyable#copy()
	 */
	@Override
	public ServerMatchpreprataionStateChangedAnnouncement copy() {
		
		final ServerMatchpreprataionStateChangedAnnouncement copy = new ServerMatchpreprataionStateChangedAnnouncement();

		copy.mMatchID = this.mMatchID;
		copy.mMatchState = this.mMatchState;
		
		return copy;
	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.server.messages.shared.AbsServerMatchMessage#onReadTransmissonMatchData(java.io.DataInputStream)
	 */
	@Override
	protected void onReadTransmissonMatchData(DataInputStream pDataInputStream)
			throws IOException {
		
		this.mMatchState = MatchPreparationState.valueOf(pDataInputStream.readUTF());

	}

	/* (non-Javadoc)
	 * @see org.anddev.andengine.extension.tbsengine.multiplayer.server.messages.shared.AbsServerMatchMessage#onWriteTransmissonMatchData(java.io.DataOutputStream)
	 */
	@Override
	protected void onWriteTransmissonMatchData(
			DataOutputStream pDataOutputStream) throws IOException {
		
		pDataOutputStream.writeUTF(this.mMatchState.name());
	}
	
	@Override
	public void setMatchPreparation(AbsServerMatchPreparation pMatch) {
		super.setMatchPreparation(pMatch);
		
		this.mMatchState = pMatch.getMatchState();
	}

	/**
	 * @return the mMatchState
	 */
	public MatchPreparationState getMatchState() {
		return mMatchState;
	}

}
